package com.wang.gulimall.product.app;

import com.wang.common.utils.PageUtils;
import com.wang.common.utils.R;
import com.wang.gulimall.product.entity.CategoryEntity;
import com.wang.gulimall.product.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 商品三级分类
 *
 * @author wangliuyang
 * @email 1842184157@qq.com
 * @date 2021-01-12 16:27:31
 */
@RestController
@RequestMapping("product/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * Desc:树形列表展示分类列表
     *
     * @param
     * @return {@link R}
     * @author wangliuyang
     * @date
     */

    //http://localhost:10000/product/category/list/tree
    @RequestMapping("/list/tree")
    public R listTree() {
        List<CategoryEntity> list = categoryService.listWithTree();
        return R.ok().put("data", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    public R info(@PathVariable("catId") Long catId) {
        CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);

//                "data": {
//            "catId": 1440,
//                    "name": "东北大学测试2",
//                    "parentCid": 1436,
//                    "catLevel": 3,
//                    "showStatus": 1,
//                    "sort": 0,
//                    "icon": "",
//                    "productUnit": "",
//                    "productCount": null

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        log.info("进行更新操作"+category);
        categoryService.updateCascade(category);
        //categoryService.updateDetail(category);
        return R.ok();
    }


    @RequestMapping("update/sort")
    public R updateSort(@RequestBody CategoryEntity[] categoryEntities) {
        categoryService.updateBatchById(Arrays.asList(categoryEntities));
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
        //categoryService.removeByIds(Arrays.asList(catIds));
        log.info("测试" + Arrays.asList(catIds));

        //1、安全检查 检查当前菜单是否被别的地方引用，引用的话还不能进行删除
        categoryService.removeMenusByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
